<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 勋章控制器
 */
class Money extends AdminBase
{  
	/**
     * 添加
     */
    public function cjEdit()
    {
        IS_POST && $this->jump($this->logicMoney->cjEdit($this->param));
        
        $info = db('cj')->where(['id' => $this->param['id']])->find();

        $this->assign('info', $info);

        return $this->fetch('cj_edit');
    }
	/**
     * 添加
     */
    public function cjAdd()
    {
        IS_POST && $this->jump($this->logicMoney->cjAdd($this->param));

        return $this->fetch('cj_edit');
    }
	 /**
     * 幸运竞猜列表
     */
    public function cjList()
    {     
		$data=$this->param;
		$where=[];
        $list=$this->logicMoney->getCjList($where,'*');
		$this->assign('list',$list );
        return $this->fetch('cj_list');
    }
    /**
     * 勋章列表
     */
    public function moneyList()
    {     
		$data=$this->param;
        $where = $this->logicMoney->getWhere($data);
		if(!empty($data['user_id'])){
			$order=" m.create_time desc";
		}else{
			$order='';
		}
        $list=$this->logicMoney->getMoneyList($where,'m.*,b.username,b.name',$order);
		$count=db('sell')->where('stat=0')->count();
		$this->assign('count',$count );
		$counts=db('money')->where('user_id=10000 and isys=0')->count();
		$this->assign('counts',$counts );
		$this->assign('list',$list );
        return $this->fetch('money_list');
    }
	/**
     * 转让列表
     */
    public function moneyZr()
    {    
		$data=$this->param;
        $where = [];
        !empty($data['search_data']) && $where['b.name|b.username'] = ['like', '%'.$data['search_data'].'%'];  
        $list=$this->logicMoney->getMoneyZrList($where);
		foreach($list as $k=>$v){
			$list[$k]['gusername']=db('user')->where('id',$v['getid'])->value('username');
		}
        $this->assign('list',$list );
        return $this->fetch('moneyzr');
    }
	/**
     * 勋章交易列表
     */
    public function moneyOrder()
    {    
		$data=$this->param;
        $where = [];
		$where['m.stat'] = 1;
        !empty($data['pay']) && $where['m.pay'] = $data['pay']; 
        !empty($data['search_data']) && $where['m.order_sn|b.name|b.username'] = ['like', '%'.$data['search_data'].'%'];  
        $list=$this->logicMoney->getMoneyOrderList($where);
		$zfb=db('money_order')->field('sum(money) as zfbmoney')->where(array('stat'=>1,'pay'=>2))->find();
		$wx=db('money_order')->field('sum(money) as wxmoney')->where(array('stat'=>1,'pay'=>3))->find();
        $this->assign('list',$list );
		$this->assign('zfb',$zfb['zfbmoney']?$zfb['zfbmoney']:0); 
		$this->assign('wx',$wx['wxmoney']?$wx['wxmoney']:0); 
        return $this->fetch('moneyorder');
    }
	/**
     * 勋章转让记录
     */
    public function moneyLogList()
    {        
		$arr=parse_config_array('moneyType');
        $where = $this->logicMoney->getWheres($this->param);
        $list=$this->logicMoney->getMoneyLogList($where,'m.*,b.username,b.name,c.username as gusername,c.name as gname','m.create_time desc');
		foreach($list as $k=>$v){
			$list[$k]['typeName']=$arr[$v['type']];
		}
        $this->assign('list',$list );      
        return $this->fetch('moneyinfo');
    }
    /**
     * 勋章添加
     */
    public function moneyAdd()
    {
		$now=db('money')->order('id desc')->value('id');
		$a=!empty($now)?$now:0;
		db('money')->startTrans(); 
        for($i=$a+1;$i<101+$a;$i++){
			if($i < 101+$a){
				$code='U'.str_pad($i,8,"0",STR_PAD_LEFT);
				$arr=array(
					'id'=>$i,
					'code'=>$code,
					'create_time'=>time(),
					'status'=>1,
					'user_id'=>10000
				);
				unset($code);
				$res=db('money')->insert($arr);
			}		
		} 
		if($res !== false){
			db('money')->commit();
		}else{
			db('money')->rollback();	
		}
		$this->redirect('money/moneyList');
        //return $this->fetch('money_list');
    }
    /**
     * 导出
     */
    public function exportMoneyList()
    {
        
        $where = $this->logicMoney->getWhere($this->param);
        
        $this->logicMoney->exportMoneyList($where);
    }
	/**
     * 状态改变
     */
    public function statChange(){
		return $this->jump($this->logicMoney->moneyStatus($this->param));
	}
}
